import { DeleteService } from './../services/delete.service';
import { CommonInterceptor } from "./../services/common.interceptor";
import {
    HttpCode, Controller, UseInterceptors, Post, FilesInterceptor, UploadedFiles, FileInterceptor, UploadedFile,
    Param, Body, Get,
} from '@nestjs/common';
import { MailService } from './services/mail.sevices';
import { MailerService } from './services/mailer.service';
import * as fs from 'fs';

@UseInterceptors(new CommonInterceptor())
@Controller()
export class MailController {
    constructor(
        public mailService: MailService,
        public mailerService: MailerService,
        public deleteService: DeleteService,
    ) {
    }
    @Post('uploadmail')
    async uploadMail( @Body() params) {
        await this.mailService.uploadMail(params);
        return { success: true };
    }

    @Get('uploadmail')
    async find() {
        const userMailList = await this.mailService.find()
        return {
            success: true,
            userMailList,
        };
    }
    @Post('deletemail')
    async deleteMail( @Body() params) {
        await this.mailService.deleteMail(params.name);
        return {
            success: true,
        };
    }

    @Post('send')
    async send( @Body() params) {
        return await this.mailerService.send(params);
    }

    @Post('uploadpayroll')
    @UseInterceptors(FileInterceptor('file'))
    @HttpCode(200)
    async uploadFile( @UploadedFile() file, @Body() params) {
        const data = await this.mailService.uploadPayroll(file);
        fs.unlinkSync('src/payroll/payroll.xlsx');
        return { data, date: params.date };
    }
}